$(function() {
    //主流程对象
    var mainCtrl = avalon.define({
        $id: "mainController",
        commCode: "",
        branchCode: "",
        clusterId: "",
        addTask: function() {
            var bcode = mainCtrl.branchCode,
                cinfo = urlParams.commInfo,
                rcode = urlParams.regionCode,
                cluster = urlParams.isCluster?urlParams.isCluster:0;
            var toPage = "addCluster.html?branchCode="+bcode+"&commInfo="+cinfo+"&regionCode="+rcode+"&isCluster="+cluster+ "&commCode=" + mainCtrl.commCode;
            if(mainCtrl.clusterId != ""){
                toPage +="&clusterId=" + mainCtrl.clusterId;
            }
            window.location = toPage;
        },
        returnPage: function() {
            var bcode = mainCtrl.branchCode,
                cinfo = urlParams.commInfo,
                rcode = urlParams.regionCode,
                cluster = urlParams.isCluster?urlParams.isCluster:"";
            window.location = "clusterCommManage.html?branchCode="+bcode+"&commInfo="+cinfo+"&regionCode="+rcode+"&isCluster="+cluster;
        }
    })

    var popCtrl = avalon.define({
        $id: "popController",
        confirmType: "del",
        delCommObj: "",
        confirmSure: function() {
            var dataJson = {
                "branchCode": mainCtrl.branchCode,
                "commCode": popCtrl.delCommObj.commCode
            };
            if(mainCtrl.clusterId != "") {
                dataJson = {
                    "branchCode": mainCtrl.branchCode,
                    "clusterId": mainCtrl.clusterId,
                    "commCode": popCtrl.delCommObj.commCode
                };
            }
            var curSource = [];
            for(var i = 0 ; i < clusterTable.table.sources.length; i++){
                if(clusterTable.table.sources[i].commCode == mainCtrl.commCode){
                    curSource.push(clusterTable.table.sources[i]);
                    break;
                }
            }
            $.ajax({
                type: "post",
                dataType: "json",
                data: dataJson,
                url: DI.delCluster,
                success: function(data) {
                    $("#popup_confirm").hide();
                    if(data.retFlag == 1) {
                        PLATFORM.showTips("success", "删除成功");
                        clusterTable.searchParam.pageNo = "1";
                        if(data.data == 0){
                            clusterTable.table.sources = curSource;
                        }else{
                            clusterTable.refresh();
                        }
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err)
                }
            });
        }
    });

    var clusterTable = null;
    var opts = {
        targetId: "exptTable",
        templateId: "tableSaleExpt",
        settings: {
            url: DI.queryModelCommList,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            },
            searchParam: {
                "commCode": "",
                "branchCode": "",
                "clusterId": ""
            }
        },
        customFuncs: {
            getUrl: function(commcode) {
                var codeLen = commcode.length;
                if(codeLen == 18) {
                    return DI.imageSplitURL + commcode + "_1_100x100.jpg";
                } else {
                    var curLen = 18 - codeLen,
                        curZero = "";
                    for(var i = 0; i < curLen; i++) {
                        curZero += "0";
                    }
                    return DI.imageSplitURL + curZero + "" + commcode + "_1_100x100.jpg";
                }
                return "";
            },
            delPop: function(row) {
                popCtrl.delCommObj = row;
                $("#popup_confirm").show();
            }
        }
    };

    //获取URL数据
    var urlParams = PLATFORM.getUrlArgs();
    mainCtrl.branchCode = urlParams.branchCode;
    //如果存在商品编码
    if(urlParams.commCode) {
        mainCtrl.commCode = urlParams.commCode;
        opts.settings.searchParam = {
            "commCode": mainCtrl.commCode,
            "branchCode": mainCtrl.branchCode
        }
    }
    if(urlParams.clusterId) {
        mainCtrl.clusterId = urlParams.clusterId;
        opts.settings.searchParam = {
            "branchCode": mainCtrl.branchCode,
            "clusterId": mainCtrl.clusterId
        }
    }
    clusterTable = SwiftUI.swiftTable.getInstance(opts);
});
